import { defineStore } from 'pinia'
import { ref } from 'vue'
import axios from 'axios'
import { userId, opt, change } from './user'

import img from '@/assets/1.jpg'

//获取好友头像
export const getFriendHeadShot = (url) => {
    if(opt.value === 0)
        return "https://47.120.69.214:8787/"+url
    else
        return img
}

//好友列表
export const friendList = ref([])

//获取好友列表
export const useGetFriendStore = defineStore('getFriend', () => {

    const getFriendList = async () => {
        const response = await axios({
            method : 'get',
            url : 'https://47.120.69.214:8787/chat/getGroupAndFriendList',
            params : {
                "userId" : userId.value
            },
        }) 
        .catch(error => {
            console.log(error)
        })
        friendList.value = response.data
        change(friendList.value)
    }
    
    return {
        getFriendList
    }
})

export function friendSort(sendUserId, type, receiveUserId){
    const friend = ref()
    const newFriendList = ref([])
    for(let i=0;i<friendList.value.length;i++){
        if(friendList.value[i].id === sendUserId && type === 'user'){
            friend.value = friendList.value[i]
        }
        else if(friendList.value[i].id === receiveUserId && type === 'group'){
            friend.value = friendList.value[i]
        }
        else{
            newFriendList.value.push(friendList.value[i])
        }
    }
    newFriendList.value.unshift(friend.value)
    // console.log(newFriendList.value)
    friendList.value = newFriendList.value
}